package com.koicarp.university.graudate.common.utils;

import com.koicarp.university.graudate.common.dto.MenuVueDto;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @auther liutao
 * @Date 2020-09-02 20:30:28
 */
public class UniversityTreeUtil {
    public static List<MenuVueDto> buildTree(List<MenuVueDto> nodes){
        Map<Integer,List<MenuVueDto>> children=nodes.stream().filter(MenuVueDto->MenuVueDto.getRootId()!=0)
                .collect(Collectors.groupingBy(MenuVueDto::getRootId));
        nodes.forEach(MenuVueDto->MenuVueDto.setChildren(children.get(MenuVueDto.getMenuId())));
        return nodes.stream().filter(MenuVueDto->MenuVueDto.getRootId()==0).collect(Collectors.toList());
    }
}
